package com.ysstech.etfmanage.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.Dicvalue;
import com.ysstech.common.entity.TreeNode;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.etfmanage.entity.Account;
import com.ysstech.etfmanage.enums.EtfManageEnum;
import com.ysstech.etfmanage.feign.FeignFinanceService;
import com.ysstech.etfmanage.thread.EtfAccountMainGenerator;
import com.ysstech.etfmanage.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2021-02-10
 */
@Controller
@Slf4j
@Api(tags = "ETF台账api")
@RequestMapping("/account")
public class AccountController extends BaseController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private EtfAccountMainGenerator etfAccountMainGenerator;
    @Autowired
    private FeignFinanceService feignFinanceService;

    @YssPermissions(value = {"etfmanage:account"})
    @ApiOperation(value = "跳转ETF台账管理页面")
    @YssLog(module = "ETF台账管理", remark = "跳转ETF台账管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "etfmanage/account";
    }

    /**
     * 获取初始化页面下拉选项的值
     *
     * @return RestResult
     */
    @YssPermissions(value = {"etfmanage:account"})
    @ApiOperation(value = "获取ETF台账页面机构的状态的值")
    @YssLog(module = "ETF台账管理", remark = "获取ETF台账面机构的状态的值")
    @PostMapping(value = "/init")
    @ResponseBody
    public RestResult init() throws Exception {
        Map<String, Object> map = new HashMap<>();
        //etf模型 股票，债券，期货
        List<Dicvalue> listmode = dicValueService.getCacheDicValueList(EtfManageEnum.ETF_MODE_CODE.getCode());
        map.put("etfMode", listmode);
        // etf类型  单市场，跨市场，集中申购
        List<Dicvalue> listtype = dicValueService.getCacheDicValueList(EtfManageEnum.ETF_TYPE_CODE.getCode());
        map.put("etfType", listtype);
        List<Dicvalue> listmarket = dicValueService.getCacheDicValueList(EtfManageEnum.ETF_MARKET_CODE.getCode());
        map.put("etfMarket", listmarket);
        //ETF的套账
        List<TreeNode> lsetlists = feignFinanceService.queryAccountTreeByType(redisCacheManager.getLoginUserInfo().getOrgId(), 5, null);
        map.put("lsetlists", lsetlists);
        return ResultGenerator.getSuccessResult("加载ETF字典信息成功！", map);
    }

    /**
     * 查询ETF台账列表信息
     *
     * @return
     */
    @YssPermissions(value = {"etfmanage:account"})
    @ApiOperation(value = "获取ETF台账数据")
    @YssLog(module = "ETF台账管理", remark = "获取ETF台账数据")
    @PostMapping(value = "/listInfo")
    @ResponseBody
    public RestResult queryAccount(Account account, String page, String rows) throws Exception {
        return ResultGenerator.getSuccessResult("获取ETF台账数据成功",
                accountService.queryAllEtfaccount(account, this.getPage(page, rows)));
    }


    /**
     * 更新台账
     *
     * @param account
     * @return
     */
    @YssPermissions(value = {"etfmanage:account:renewAccount"})
    @ApiOperation(value = "更新ETF台账数据")
    @YssLog(module = "ETF台账管理", remark = "更新ETF台账数据")
    @PostMapping(value = "/renewAccount")
    @ResponseBody
    public RestResult renewAccount(Account account) throws Exception {
        if (StringUtils.isBlank(account.getAccountId())) {
            throw new BusinessException("获取基金信息失败，请稍后重试！");
        }
        List<String> list = JSONArray.parseArray(account.getAccountId(), String.class);
        etfAccountMainGenerator.renewAccount(list, account);
        return ResultGenerator.getSuccessResult("更新ETF台账数据成功！");
    }


}
